package com.cidp.system.service.impl;

import java.util.regex.Pattern;

import com.cidp.system.common.Constants;
import com.cidp.system.dao.ISystemInfoDao;
import com.cidp.system.model.Tsysteminfo;
import com.cidp.system.service.ISystemInfoService;
import com.litt.core.common.Utility;
import com.litt.core.exception.BusiException;
import com.litt.core.page.IPageList;
import com.litt.core.page.PagePoParam;

/**
 * <b>标题：</b>系统配置管理Service实现.
 * 
 * <pre>
 * &lt;b&gt;描述&lt;/b&gt;
 *    系统配置管理
 * </pre>
 * 
 * @author lijian
 * @since 2008-5-23
 * @version 1.0
 */
public class SystemInfoServiceImpl implements ISystemInfoService {

	private ISystemInfoDao systemInfoDao;
	
	/**
	 * 读取系统配置信息
	 * 
	 * @param sysId 系统配置ID
	 * @return Tsysteminfo对象
	 * @throws BusiException 
	 */
	public Tsysteminfo loadById(Long sysId) throws BusiException{
		if(sysId != null)
			return systemInfoDao.loadById(sysId);
		else
			throw new BusiException("传入参数为空");
	}

	/**
	 * 删除系统配置信息.
	 * 
	 * @param systeminfo 系统配置对象
	 */
	public void deleteSystemInfo(Tsysteminfo systeminfo){
		systemInfoDao.deleteSystemInfo(systeminfo);
	}
	
	/**
	 * 删除系统配置信息.
	 * 
	 * @param id 系统配置Id
	 * @throws BusiException 
	 */
	public void deleteSystemInfoById(Long id) throws BusiException{
		if(id != null){
			Tsysteminfo systeminfo = systemInfoDao.loadById(id);
			systemInfoDao.deleteSystemInfo(systeminfo);
		}
		else
			throw new BusiException("传入参数为空");
	}

	/**
	 * 保存系统配置信息.
	 * 
	 * @param systeminfo 系统配置对象
	 * @throws BusiException 
	 */
	public void saveSystemInfo(Tsysteminfo systeminfo) throws BusiException{
		if(!Pattern.compile(Constants.EMAIL).matcher(systeminfo.getSystemEmail()).find())
			throw new BusiException("Email格式不正确");
		if(systeminfo.getSmtpAuthFlag().intValue() == 0){
			if(Utility.isEmpty(systeminfo.getSmtpUser()))
				throw  new BusiException("系统邮件用户名为空");
			if(Utility.isEmpty(systeminfo.getSmtpPass()))
				throw  new BusiException("系统邮件密码为空");
		}
		systemInfoDao.saveSystemInfo(systeminfo);
	}

	/**
	 * 更新系统配置信息.
	 * 
	 * @param systeminfo 系统配置对象
	 * @throws BusiException 
	 */
	public void updateSystemInfo(Tsysteminfo systeminfo) throws BusiException{
		if(!Pattern.compile(Constants.EMAIL).matcher(systeminfo.getSystemEmail()).find())
			throw new BusiException("Email格式不正确");
		if(systeminfo.getSmtpAuthFlag().intValue() == 1){
			if(Utility.isEmpty(systeminfo.getSmtpUser()))
				throw  new BusiException("系统邮件用户名为空");
			if(Utility.isEmpty(systeminfo.getSmtpPass()))
				throw  new BusiException("系统邮件密码为空");
		}
		systemInfoDao.updateSystemInfo(systeminfo);
	}

	/**
	 * 分页查询系统信息信息.
	 * 
	 * @param pagePoParam 查询参数
	 * @return IPageList IPageList对象
	 * @throws BusiException
	 */
	public IPageList listByPage(PagePoParam pagePoParam) throws BusiException {
		if(pagePoParam != null)
			return systemInfoDao.listByPage(pagePoParam);
		else
			throw new BusiException("传入参数为空");
	}
	
	/**
	 * 读取系统配置信息
	 * 
	 * @return Tsysteminfo对象
	 */
	public Tsysteminfo getTsysteminfo(){
		return systemInfoDao.getTsysteminfo();
	}

	
	public void setSystemInfoDao(ISystemInfoDao systemInfoDao) {
		this.systemInfoDao = systemInfoDao;
	}
}
